package com.ebt.m.homepage;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.BaseListView;
import com.ebt.m.data.bean.Brand;
import com.ebt.m.event.HomeUpdateSearchLabelEvent;
import com.ebt.m.commons.buscomponent.listview.BasePresenter;
import com.ebt.m.presenter.IProductSerachListView;
import com.ebt.m.presenter.ProductSearchPresenter;
import com.ebt.m.utils.RecyclerViewDivider;
import com.ebt.m.commons.buscomponent.listview.ISectionFactory;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.wiki.view.ProductItemView1;

import org.greenrobot.eventbus.EventBus;

import static com.ebt.m.wiki.view.ProductItemView1.DATATYPE_REGISTED;


public class ProductSearchListView extends BaseListView<ProductSearchPresenter> implements IProductSerachListView {

    public ProductSearchListView(Context context) {
        this(context, null, 0);
    }

    public ProductSearchListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProductSearchListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mRecyclerView.addItemDecoration(new RecyclerViewDivider(context, LinearLayoutManager.HORIZONTAL, (int) getResources().getDimension(R.dimen.common_divider_dimen), getResources().getColor(R.color.common_divider_color)));
        mRefreshLayout.setRefreshNeeded(false);
    }

    private int mFlag;

    public void addFlag(int flag) {
        mFlag = flag;
    }

    @Override
    protected ISectionFactory createViewFactory() {
        ISectionFactory factory = new ISectionFactory() {
            @Override
            public IView createView(int viewType) {
                ProductItemView1 sectionProductView = new ProductItemView1(getContext(), DATATYPE_REGISTED);
                sectionProductView.addFlag(mFlag);
                return sectionProductView;
            }
        };
        return factory;
    }

    @Override
    protected ProductSearchPresenter createPresenter() {
        return new ProductSearchPresenter(getContext(), this);
    }

    @Override
    public void update(Object... args) {
        super.update();
        mPresenter.loadNew(args);
    }

    @Override
    public void loadNext() {
        mPresenter.loadNext();
    }

    @Override
    protected View configEmptyView() {
        View emptyContent = LayoutInflater.from(getContext()).inflate(R.layout.layout_product_search_empty, null);
        TextView textView = (TextView) emptyContent.findViewById(R.id.mession_empty);
        textView.setText(R.string.product_search_null);
        return emptyContent;
    }

    @Override
    protected boolean createFootView() {
        return true;
    }

    @Override
    public void updateSearchLabel(Brand brand) {
        HomeUpdateSearchLabelEvent labelEvent = new HomeUpdateSearchLabelEvent();
        labelEvent.brand = brand;
        EventBus.getDefault().post(labelEvent);
    }
}
